#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

static int is_running = 1;

static void use_resource(void){
    printf("use resource\n");
}

static void free_resource(void){
    printf("free resource\n");
}

void signal_handler(int sig){
    if (sig == SIGINT)
    {
        printf("ctrl + c 结束\n");
        is_running = 0;
    }else{
        printf("kill结束\n");
        free_resource();
        exit(EXIT_SUCCESS);
    }
}

int main(int argc, char const *argv[])
{
    // 注册信号处理函数
    signal(SIGINT, signal_handler); // ctrl + c
    signal(SIGTERM, signal_handler); // kill

    use_resource();

    while (is_running)
    {
        printf("running\n");
        sleep(1);
    }
    
    free_resource();

    return 0;
}
